package com.woniuxy.repay.aspect;

import cn.hutool.extra.mail.MailUtil;
import com.woniuxy.entity.RepaymentLog;
import com.woniuxy.repay.annotation.MonitorLog;
import com.woniuxy.repay.mapper.RepaymentLogMapper;
import com.woniuxy.repay.tools.HttpContextUtils;
import com.woniuxy.repay.tools.IPUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;


/**
 * @author shichen
 * @create 2021-11-24-16:05
 * @description
 */
@Aspect
@Component
public class LogAspect {

    @Resource
    private RepaymentLogMapper repaymentLogMapper;

    @Pointcut("@annotation(com.woniuxy.repay.annotation.MonitorLog)")
    public void pointcut() {
    }

    @Around("pointcut()")
    public Object around (ProceedingJoinPoint point) {
        long beginTime = System.currentTimeMillis();
        Object obj = null;
        try {
            // 执行方法
            obj = point.proceed();
        }catch (Throwable e) {
            e.printStackTrace();
            String msg = "你写的程序出bug了 : " + e.toString() + new Date().toString();
            MailUtil.send("1282399009@qq.com", "bug 来了!", msg, false);
        }
        // 执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        // 保存日志
        saveLog(point, time);


        return obj;
    }

    private void saveLog(ProceedingJoinPoint joinPoint, long time) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        RepaymentLog sysLog = new RepaymentLog();
        MonitorLog logAnnotation = method.getAnnotation(MonitorLog.class);
        if (logAnnotation != null) {
            // 注解上的描述
            sysLog.setOperation(logAnnotation.value());
        }
        // 请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = signature.getName();
        sysLog.setMethod(className + "." + methodName + "()");
        // 请求的方法参数值
        Object[] args = joinPoint.getArgs();
        // 请求的方法参数名称
        LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
        String[] paramNames = u.getParameterNames(method);
        if (args != null && paramNames != null) {
            String params = "";
            for (int i = 0; i < args.length; i++) {
                params += "  " + paramNames[i] + ": " + args[i];
            }
            sysLog.setParams(params);
        }
        // 获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        // 设置IP地址
        sysLog.setIp(IPUtils.getIpAddr(request));
        // 模拟一个用户名
        sysLog.setUserName("mrbird");
        sysLog.setConsumeTime((int) time);
        Date date = new Date();
        sysLog.setCreateTime(date);
        // 保存系统日志
        repaymentLogMapper.insertByCondition(sysLog);
    }
}
